#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include "package.h"

pthread_t mq_pids;
int sock = 0;

void *create_pop_message_thread_handle(void *arg){
    signal(SIGPIPE, SIG_IGN);
    while(1){
    	//监听返回
		char ret_package[PACKAGE_BUFF_MAX_LEN + 1] = { 0 };
		PACKAGE_HEAD head;
		if(recv(sock, &head, sizeof(head) , 0) <= 0){
			printf("recv package head error\n");
			close(sock);
			return 0;
		}
		int package_len = ntohl(head.package_len);
		int head_len = ntohs(head.head_len);
		int version = ntohs(head.version);
		int command = ntohl(head.command);
		int idx = ntohl(head.idx);
		int body_len = package_len - head_len;
		memcpy(ret_package , &head , sizeof(head));
		int len = 0;
		while(len < body_len){
			int ret = recv(sock, ret_package + sizeof(head) + len , body_len - len , 0);
			if(ret == -1){
				printf("recv package error\n");
				close(sock);
				return 0;
			}
			len += ret;
		}

		printf("retpackage head.package_len:%d\n" , package_len);
		printf("retpackage head.head_len:%d\n" , head_len);
		printf("retpackage head.version:%d\n" , version);
		printf("retpackage head.command:%d\n" , command);
		printf("retpackage head.idx:%d\n" , idx);
		printf("retpackage body:%s\n" , ret_package + sizeof(head));
    }
    return 0;
}

int main(){
	

	//创建套接字
	sock = socket(AF_INET, SOCK_STREAM, 0);
	struct sockaddr_in serv_addr;
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = inet_addr("192.168.0.119");
	serv_addr.sin_port = htons(20000);
	
	//向服务器发送连接请求
	if(connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1){
		printf("connect server error\n");
		close(sock);
		return 0;
	}

	//开启消息监听线程
    pthread_create(&mq_pids, NULL, create_pop_message_thread_handle, NULL);

	while(1){
		//发送并接收数据
		char package[PACKAGE_BUFF_MAX_LEN + 1] = { 0 };
		char buffer[PACKAGE_BUFF_MAX_LEN - PACKAGE_HEAD_LEN + 1] = { 0 };
		int cmd = 0;
		printf("输入包类型[输入0退出程序]：\n");
		printf("#define COM_HEARTBEAT           1\n");
		printf("#define COM_MESSAGE             100\n");
		printf("#define COM_RE_MESSAGE          101\n");
		printf("#define COM_OFF_MESSAGE         102\n");
		printf("#define COM_RE_OFF_MESSAGE      103\n");
		printf("#define COM_REGISTER            200\n");
		printf("#define COM_RE_REGISTER         201\n");
		printf("#define COM_LOGIN               202\n");
		printf("#define COM_RE_LOGIN            203\n");
		printf("#define COM_FINDFRIEND          204\n");
		printf("#define COM_RE_FINDFRIEND       205\n");
		printf("#define COM_LOGOUT              206\n");
		printf("#define COM_RE_LOGOUT           207\n");
		printf("#define COM_LOGOUT              206\n");
		printf("#define COM_REPASS              208\n");
		printf("#define COM_RE_REPASS           209\n");
		printf("#define COM_OPERATEFRIEND       210\n");
		printf("#define COM_RE_OPERATEFRIEND    211\n");
		printf("#define COM_CONFIRMFRIEND       212\n");
		printf("#define COM_RE_CONFIRMFRIEND    213\n");
		printf("#define COM_DELFRIEND           214\n");
		printf("#define COM_RE_DELFRIEND        215\n");
		printf("#define COM_FRIENDLIST          216\n");
		printf("#define COM_RE_FRIENDLIST       217\n");
		printf("#define COM_USERINFO            218\n");
		printf("#define COM_RE_USERINFO         219\n");
		printf("#define CLI_MESSAGE             800\n");
		printf("#define CLI_OPERATEFRIEND       801\n");
		printf("#define CLI_CONFIRMFRIEND       802\n");
		printf("#define CLI_DELFRIEND           803\n");
		
		scanf("%d", &cmd);
		if(cmd == 0)
			break;

		printf("输入包体内容：\n");
		scanf("%s", buffer);


		//发送数据
		package_new(buffer , strlen(buffer) , 1 , cmd , package);
		if(send(sock, package, sizeof(buffer) + PACKAGE_HEAD_LEN , 0) == -1){
			printf("send package error\n");
			close(sock);
			return 0;
		}

		if(cmd == 1){
			printf("心跳包已发送，该包无返回\n");
			continue;	//心跳包不返回
		}


		
	}

	//断开连接
	close(sock);

	return 0;
}